package com.cy.aircompressor.main.ui.Infoover.provider;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.cy.aircompressor.config.AirConfig;
import com.cy.aircompressor.databinding.ItemCompressorLayoutBinding;
import com.cy.aircompressor.entity.compressor.Compressor;
import com.cy.aircompressor.entity.select.SelectDict;
import com.cy.aircompressor.entity.sensor.SensorItemData;
import com.cy.aircompressor.entity.sensor.SensorLatestData;
import com.cy.aircompressor.main.ui.Infoover.view.StageCylinderView;
import com.drug.lib.util.DLog;
import com.drug.lib.view.recycptr.ItemViewBinder;
import com.net.model.base.BaseBindViewHolder;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class CompressorItemProvider extends ItemViewBinder<Compressor, CompressorItemProvider.ViewHolder> {
    Context mContext;

    public CompressorItemProvider(Context mContext) {
        this.mContext = mContext;
    }

    @NonNull
    @Override
    protected CompressorItemProvider.ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        return new ViewHolder(ItemCompressorLayoutBinding.inflate(inflater, parent, false));
    }

    @Override
    protected void onBindViewHolder(@NonNull CompressorItemProvider.ViewHolder holder, @NonNull Compressor entity) {
        holder.mBinding.airName.setText(entity.getName());

        List<SelectDict> compressorParts = AirConfig.get().compressorParts;
        if (compressorParts != null) {
            holder.mBinding.stageCylinderView.removeAllViews();
            for (SelectDict compressorPart : compressorParts) {
                StageCylinderView stageCylinderView = new StageCylinderView(mContext);
                SensorLatestData sensorLatestData = entity.getSensorLatestData();
                if (sensorLatestData == null) {
                    stageCylinderView.setStatus(0);
                    stageCylinderView.setTitleAndName(compressorPart.getLabel(), null);
                } else {
                    SensorItemData sensorItemData = getSensorItemData(compressorPart, sensorLatestData.getDataList());
                    if (sensorItemData == null) {
                        stageCylinderView.setStatus(0);
                        stageCylinderView.setTitleAndName(compressorPart.getLabel(), null);
                    } else {
                        stageCylinderView.setStatus(Integer.parseInt(sensorItemData.getStatus()));
                        stageCylinderView.setTitleAndName(sensorItemData.getCompressorPartDesc(), sensorItemData.getV());
                    }
                }
                addView(holder.mBinding.stageCylinderView, stageCylinderView);

            }
        }
    }

    private void addView(ViewGroup parent, View childView) {
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.topMargin = 5;
        parent.addView(childView, layoutParams);
    }

    private SensorItemData getSensorItemData(SelectDict compressorPart, List<SensorItemData> dataList) {
        if (dataList == null)
            return null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            List<SensorItemData> sensorItemDataList = dataList.stream()
                    .filter(new Predicate<SensorItemData>() {
                        @Override
                        public boolean test(SensorItemData sensorItemData) {
                            return TextUtils.equals(sensorItemData.getCompressorPartCode(), compressorPart.getValue()) &&
                                    TextUtils.equals(sensorItemData.getCompressorPointCode(), "temperature");
                        }
                    }).collect(Collectors.toList());
            DLog.e("===============getSensorItemData========================" + sensorItemDataList.size());
            if (sensorItemDataList != null && sensorItemDataList.size() > 0) {
                return sensorItemDataList.get(0);
            }
        }
        return null;

    }

    public class ViewHolder extends BaseBindViewHolder<ItemCompressorLayoutBinding> {

        protected ViewHolder(ItemCompressorLayoutBinding itemView) {
            super(itemView);
        }
    }
}
